package cn.dengfanxin.gae.test.keyword;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteKeyword extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7949467470938263718L;

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");

		if (id != null && !id.equals("")) {
			try {
				Long l = Long.parseLong(id);
				PersistenceManager pm = PMF.getInstance()
						.getPersistenceManager();
				try {
					Keyword k = pm.getObjectById(Keyword.class, l);
					if (k == null) {
						out.print("fail");
					} else {
						pm.deletePersistent(k);
						out.print("success");
					}
				} finally {
					pm.close();
				}
			} finally {
				// do nothing
			}
		} else
			out.print("fail");
		out.flush();
		out.close();
	}

}
